package com.ww.scm.repository.mat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import com.ww.core.exception.ServiceException;
import com.ww.core.orm.PropertyFilter;
import com.ww.core.orm.PropertyFilter.MatchType;
import com.ww.core.orm.hibernate.HibernateDao;
import com.ww.core.utils.DateUtils;
import com.ww.scm.constants.Constants;
import com.ww.scm.entity.mat.Storage;
import com.ww.scm.entity.mat.TransactionDetail;
import com.ww.scm.entity.pub.Dictionary;

//Spring Bean的标识.
@Component
public class StorageRepository  extends HibernateDao<Storage, String> {


	public Storage createOrUpdateStorage(TransactionDetail transactionDetail,boolean output,String operator) {
		Date now=new Date();
		
		//Storage storage=findUniqueBy("materiel.id", materiel.getId());
		/*
		 * 第一种
		List<PropertyFilter> filters=new ArrayList<PropertyFilter>();
		filters.add(new PropertyFilter("EQS_materiel.id",materiel.getId()));
		filters.add(new PropertyFilter("EQS_state.id",state.getId()));
		filters.add(new PropertyFilter("EQS_originEntity.id",transactionDetail.getOriginEntity().getId()));
		List<Storage> storageList=find(filters);
		第二种
		List<PropertyFilter> filters=new ArrayList<PropertyFilter>();
		filters.add(new PropertyFilter(MatchType.EQ,"materiel.id",materiel.getId()));
		filters.add(new PropertyFilter(MatchType.EQ,"state.id",state.getId()));
		filters.add(new PropertyFilter(MatchType.EQ,"entity.id",transactionDetail.getOriginEntity().getId()));
		List<Storage> storageList=find(filters);
		第三种
		String hql="select s from Storage s where s.materiel.id=? and s.state.id=? and s.originEntity.id=?";
		List<Storage> storageList=find(hql, materiel.getId(),state.getId(),transactionDetail.getOriginEntity().getId());
		
		第四种
		String hql="select s from Storage s where s.materiel.id=:materielId and s.state.id=:stateId and s.originEntity.id=:originEntityId";
		Map<String, String> filters=new HashMap<String, String>();
		filters.put("materielId", materiel.getId());
		filters.put("stateId", state.getId());
		filters.put("originEntityId", transactionDetail.getOriginEntity().getId());
		List<Storage> storageList=find(hql, filters);
		
		//第五中,直接用Criteria不包装.
		Criteria criteria=getSession().createCriteria(Storage.class)
		.add(Restrictions.eq("materiel.id", materiel.getId()))
		.add(Restrictions.eq("state.id", state.getId()))
		.add(Restrictions.eq("originEntity.id", transactionDetail.getOriginEntity().getId()));
		List<Storage> storageList=criteria.list();
		
		*/
		Dictionary materiel=transactionDetail.getMateriel();
		if (materiel==null){
			logger.error("创建帐目时物料对象为空!");
			throw new ServiceException("创建帐目时物料对象为空!");
		}
		Dictionary state=transactionDetail.getState();
		Dictionary originEntity= transactionDetail.getOriginEntity();
		List<PropertyFilter> filters=new ArrayList<PropertyFilter>();
		filters.add(new PropertyFilter(MatchType.EQ,"materiel.id",materiel.getId()));
		if (state!=null){
			filters.add(new PropertyFilter(MatchType.EQ,"state.id",state.getId()));
		}
		if (originEntity!=null){
			filters.add(new PropertyFilter(MatchType.EQ,"entity.id",originEntity.getId()));
		}
		Storage storage=findUnique(filters);
		if (storage==null){
			storage=new Storage();
			storage.setState(state);
			storage.setMateriel(materiel);
			storage.setEntity(originEntity);
			storage.setCreator(operator);
			storage.setCreateTime(DateUtils.formatDateHms(now));
			storage.setSysFlag(Constants.SYS_FLAG_USEING);
		}
		Double amount=transactionDetail.getAmount();
		if (output==false){
			amount=-amount;
		}
		storage.setAmount(storage.getAmount()+amount);
		storage.setLastModifier(operator);
		storage.setLastModifiedTime(DateUtils.formatDateHms(now));
		save(storage);
		return storage;
	}
	
	public Storage rollbackStorage(TransactionDetail transactionDetail,boolean output,String operator) {
		Date now=new Date();
		Storage storage=transactionDetail.getStorage();
		Double amount=transactionDetail.getAmount();
		if (output){
			amount=-amount;
		}
		storage.setAmount(storage.getAmount()+amount);
		storage.setLastModifier(operator);
		storage.setLastModifiedTime(DateUtils.formatDateHms(now));
		save(storage);
		return storage;
	}
}
